package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.YcData;
import com.ruoyi.system.domain.YcData_v2;
import com.ruoyi.system.service.IYcDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 油厂数据管理Controller
 *
 * @author ruoyi
 * @date 2022-10-23
 */
@RestController
@RequestMapping("/system/data")
public class YcDataController extends BaseController
{
    @Autowired
    private IYcDataService ycDataService;

    /**
     * 查询油厂数据管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:data:list')")
    @GetMapping("/list")
    public TableDataInfo list(YcData ycData)
    {
        startPage();
        List<YcData> list = ycDataService.selectYcDataList(ycData);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('system:data:list')")
    @GetMapping("/listMore")
    public TableDataInfo listMore(YcData_v2 ycData_v2)
    {
        startPage();
        List<YcData> list = ycDataService.selectYcDataListMore(ycData_v2);
        return getDataTable(list);
    }


    /**
     * 导出油厂数据管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:data:export')")
    @Log(title = "油厂数据管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, YcData ycData)
    {
        List<YcData> list = ycDataService.selectYcDataListUseExport(ycData);
        ExcelUtil<YcData> util = new ExcelUtil<YcData>(YcData.class);
        util.exportExcel(response, list, "油厂数据管理数据");
    }

    /**
     * 获取油厂数据管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:data:query')")
    @GetMapping(value = "/{dataId}")
    public AjaxResult getInfo(@PathVariable("dataId") Long dataId)
    {
        return AjaxResult.success(ycDataService.selectYcDataByDataId(dataId));
    }

    /**
     * 新增油厂数据管理
     */
    @PreAuthorize("@ss.hasPermi('system:data:add')")
    @Log(title = "油厂数据管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody YcData ycData)
    {
        return toAjax(ycDataService.insertYcData(ycData));
    }

    /**
     * 修改油厂数据管理
     */
    @PreAuthorize("@ss.hasPermi('system:data:edit')")
    @Log(title = "油厂数据管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody YcData ycData)
    {
        return toAjax(ycDataService.updateYcData(ycData));
    }

    /**
     * 批量修改油厂措施
     */
    @PreAuthorize("@ss.hasPermi('system:measures:edit')")
    @Log(title = "油厂数据管理", businessType = BusinessType.UPDATE)
    @GetMapping("/edits/{dataIds}")
    public void edits(@PathVariable Long[] dataIds)
    {
        ycDataService.updateYcDataByDataIds(dataIds);
    }

    /**
     * 删除油厂数据管理
     */
    @PreAuthorize("@ss.hasPermi('system:data:remove')")
    @Log(title = "油厂数据管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{dataIds}")
    public AjaxResult remove(@PathVariable Long[] dataIds)
    {
        return toAjax(ycDataService.deleteYcDataByDataIds(dataIds));
    }

    @GetMapping("/ycdatayearSelect")
    public AjaxResult ycdatayearSelect()
    {
        return AjaxResult.success(ycDataService.selectYcDataYear());
    }

    @GetMapping("/ycdatamonthSelect")
    public AjaxResult ycdatamonthSelect()
    {
        return AjaxResult.success(ycDataService.selectYcDataMonth());
    }


    @Log(title = "油厂数据管理", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('system:data:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<YcData> util = new ExcelUtil<YcData>(YcData.class);
        List<YcData> ycDataList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = ycDataService.importData(ycDataList, updateSupport, operName);
        return AjaxResult.success(message);
    }

    /**
     * 一键发布数据
     */
    @PostMapping("/importDatas")
    public void importDatas(@RequestBody YcData ycData){
        ycDataService.importDatas(ycData);
    }


    /**
     * 一键发布数据-厂
     */
    @PostMapping("/importDatas1")
    public void importDatas1(@RequestBody YcData ycData){
        ycDataService.importDatas1(ycData);
    }

    /**
     * 一键发布数据-区
     */
    @PostMapping("/importDatas2")
    public void importDatas2(@RequestBody YcData ycData){
        ycDataService.importDatas2(ycData);
    }

    /**
     * 一键发布数据-站
     */
    @PostMapping("/importDatas3")
    public void importDatas3(@RequestBody YcData ycData){
        ycDataService.importDatas3(ycData);
    }

    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<YcData> util = new ExcelUtil<YcData>(YcData.class);
        util.importTemplateExcel(response, "油厂数据");
    }
}
